package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.RandomUuidUtil;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.convert.AppCovert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.entity.App;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.mapper.AppMapper;
import com.huiminpay.merchant.mapper.MerchantMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.sql.BatchUpdateException;
import java.util.List;

/**
 * @ClassName: AppServiceImpl
 * @Package: com.huiminpay.merchant.service
 * @Description: 应用实现
 * @Author 谢同学
 * @Create 2023/11/20 19:01
 * @Version 1.0
 */
@Slf4j //日志
@Service //dubbo
public class AppServiceImpl implements AppService {

    @Resource
    private AppMapper appMapper;

    @Resource
    private MerchantMapper merchantMapper;
    /**
     * 商户添加应用信息
     *
     * @param merchantId 商户id
     * @param appDTO     应用信息
     * @return
     * @throws BatchUpdateException
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException {
       //效验商户是否通过资质审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        //校验与商户是否合法
        if (merchant == null || appDTO == null || StringUtil.isBlank(appDTO.getAppName())){
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        //校验商户使用通过资质校验
        if (merchant.getAuditStatus() == null || !"2".equals(merchant.getAuditStatus())) {
            throw new BusinessException(CommonErrorCode.E_200003); //
        }
        //应用名称唯一
        if (isExistAppName(appDTO.getAppName())) {
            throw new BusinessException(CommonErrorCode.E_200004);
        }
        //保存应用信息
        appDTO.setAppId(RandomUuidUtil.getUUID()); //生成应用id
        appDTO.setMerchantId(merchant.getId());  //  商户id
        //dto转entity
        App app = AppCovert.INSTANCE.dto2entity(appDTO);
        appMapper.insert(app);

        return AppCovert.INSTANCE.entity2dto(app);

    }

    /**
     * 查询应用名是否唯一
     * @param appName 应用名称
     * @return true：存在 false:不存在
     */
    boolean isExistAppName(String appName){
        //构建查询语句
        LambdaQueryWrapper<App> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(App::getAppName, appName);

        Integer count = appMapper.selectCount(queryWrapper);
        return count > 0;
    }



    /**
     * 查询商户下的应用列表
     * @param merchantId 商户id
     * @return
     * @throws BatchUpdateException
     */
    @Override
    public List<AppDTO> queryAppByMerchant(Long merchantId) throws BusinessException {
        List<App> apps = appMapper.selectList(new LambdaQueryWrapper<App>()
                .eq(App::getMerchantId, merchantId));
        //entity转dto
        return AppCovert.INSTANCE.listEntity2dto(apps);

    }

    /**
     * 根据应用id查询应用信息
     *
     * @param appId 应用id
     * @return 应用信息
     * @throws BusinessException
     */
    @Override
    public AppDTO getAppById(String appId) throws BusinessException {
        return AppCovert.INSTANCE.entity2dto(appMapper.selectOne(new LambdaQueryWrapper<App>()
                .eq(App::getAppId, appId)));
    }

    /**
     * 查询应用是否属于该商户
     *
     * @param merchantId 商户Id
     * @return 属于返回true 不属于返回false
     * @throws BusinessException
     */
    @Override
    public boolean queryAppInMerchantId(String appId,Long merchantId) throws BusinessException {
        return appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppId,appId).eq(App::getMerchantId,merchantId))>0;
    }
}
